package com.tongbo.mobilesafe.engine;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

/**
 * 保证GPSInfoProvider只有一个实例
 * @author wangtongbo
 *
 */
public class GPSInfoProvider {
	private LocationManager manager;
	private static GPSInfoProvider mGpsInfoProvider;
	private static Context context;
	private static MyLocationListener listener;
	//私有化构造方法
	private GPSInfoProvider(){}
	//提供一个静态的方法，提供GPSInfoProvider的实例
	public static synchronized GPSInfoProvider getInstance(Context context){
		if(null==mGpsInfoProvider){
			mGpsInfoProvider = new GPSInfoProvider();
			GPSInfoProvider.context = context;
		}
		return mGpsInfoProvider;
	}
	/**
	 * 获取gps信息
	 * @return
	 */
	public String getLocation(){
		manager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
		//manager.getAllProviders();//gps wifi
		String provider = getProvider(manager);
		//注册位置的监听器
		manager.requestLocationUpdates(provider, 60000, 50, getListener());
		SharedPreferences sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
		String location = sp.getString("location", "");
		
		return location;
	}
	/**
	 * 停止gps监听
	 */
	public void stopGpsListener(){
		manager.removeUpdates(getListener());
		
	}
	private synchronized MyLocationListener getListener(){
		if(null==listener){
			listener = new MyLocationListener();
		}
		return listener;
	}
	private class MyLocationListener implements LocationListener{
		/**
		 * 当手机位置发生改变时候调用的方法
		 */
		@Override
		public void onLocationChanged(Location location) {
			String latitude = "latitude "+location.getLatitude();//纬度
			String longitude = "longitude "+location.getLongitude();//经度
			
			SharedPreferences sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
			Editor editor = sp.edit();
			editor.putString("location", latitude+" - "+longitude);
			editor.commit();//最后一次获取的位置信息存放到SharedPreferences里面
		}
		/**
		 * 某个设备被禁用
		 */
		@Override
		public void onProviderDisabled(String provider) {
			
		}
		/**
		 * 某个设备被打开
		 */
		@Override
		public void onProviderEnabled(String provider) {
			
		}
		/**
		 * 某一个设备的状态发生改变时调用的方法 可用-->不可用 不可用-->可用
		 */
		@Override
		public void onStatusChanged(String provider, int status, Bundle extras) {
			
		}
	}
	
	/**
	 * 
	 * @param manager 位置管理服务
	 * @return 最好的位置提供者
	 */
	private String getProvider(LocationManager manager){
		Criteria criteria = new Criteria();
		criteria.setAccuracy(Criteria.ACCURACY_FINE);
		criteria.setAltitudeRequired(false);
		criteria.setPowerRequirement(Criteria.POWER_MEDIUM);
		criteria.setSpeedRequired(true);
		criteria.setCostAllowed(true);
		return manager.getBestProvider(criteria, true);
	}
}
